package com.abc.scholarship.service.impl;

import com.abc.scholarship.common.enums.StatusEnum;
import com.abc.scholarship.domain.entity.Major;
import com.abc.scholarship.exception.BusinessException;
import com.abc.scholarship.mapper.MajorMapper;
import com.abc.scholarship.service.MajorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 专业表 服务实现类
 * </p>
 *
 * @author jzw
 * @since 2024-07-02
 */
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements MajorService {

    @Autowired
    private MajorMapper majorMapper;

    @Override
    public List<Major> getNotExitByNames(List<String> names) {
        Set<String> collect = majorMapper.selectList(new LambdaQueryWrapper<Major>()
                .select(Major::getMajorName)
                .in(Major::getMajorName, names)
        ).stream().map(Major::getMajorName).collect(Collectors.toSet());

        return new ArrayList<>(
                names.stream()
                        .filter(item -> !collect.contains(item))
                        .map(Major::new)
                        .collect(Collectors.toSet())
        );
    }

    @Override
    public void setStatusByIds(List<Long> idList, Integer status) {
        majorMapper.setStatusByIds(idList,status);
    }

    @Override
    public void MUpdateById(Major major) {
        if(major.getStatus() == null){
            majorMapper.updateById(major);
            return;
        }
        if(major.getStatus().equals(StatusEnum.FROZEN.getValue())){
            major.setStatus(StatusEnum.NORMAL.getValue());
        }else if(major.getStatus().equals(StatusEnum.NORMAL.getValue())){
            major.setStatus(StatusEnum.FROZEN.getValue());
        }else throw new BusinessException("状态异常，请联系管理员");
        majorMapper.updateById(major);
    }
}
